﻿using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace MediaAssistant.DAL.Helper
{
    public class SystemHelper
    {
        public const string ApplicationName = "Media Assistant";

        public static void LocateInWndowsExplorer(string fullPath)
        {
            if (!File.Exists(fullPath))
            {
                return;
            }
            var argument = string.Format(@"/select, ""{0}""", fullPath);
            Process.Start("explorer.exe", argument);
        }

        public static string GetVersion()
        {
            var name = Assembly.GetExecutingAssembly().GetName();
            return name.Version.ToString();
        }

        public static string GetLocalApplicationData()
        {
            var localApplicationData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),ApplicationName);
            if (Directory.Exists(localApplicationData) == false)
                Directory.CreateDirectory(localApplicationData);
            return localApplicationData;
        }

        public static string GetFolderPath(string initialFolder)
        {
            var folderDialog = new FolderBrowserDialog();
            if (String.IsNullOrEmpty(initialFolder)==false)
            {
                folderDialog.SelectedPath = initialFolder;
            }
            var dialogResult = folderDialog.ShowDialog();
            return dialogResult != DialogResult.OK ? null : folderDialog.SelectedPath;
        }


        public static void PlayMovie(string fullPath)
        {
            if (!File.Exists(fullPath))
            {
                return;
            }
            Process.Start("explorer.exe", string.Format(@"""{0}""", fullPath));
        }

        public static void OpenUrl(string url)
        {
            Process.Start(url);
        }

        public static string GetAppDataLocation()
        {
            var appDataLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), ApplicationName);
            if(!Directory.Exists(appDataLocation))
            {
                Directory.CreateDirectory(appDataLocation);
            }
            return appDataLocation;
        }

        public static void Start(string filename)
        {
            Process.Start(filename);
        }

        public static void StartAndWait(string filename)
        {
            var process = Process.Start(filename);
            if(process!=null)
                process.WaitForExit();
        }
    }
}
